package com.finance.logistics.anlyzier;


import com.finance.base.constant.BaseConstants;
import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.system.bean.pojo.Organization;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 接单机构解析类
 * Created by CBWL on 2017/4/27.
 */
@Component
public class AcceptOrganizationHandler extends AbstractHandler {

    @Override
    @Resource(name = "statusHandler")
    public void setNextHandler(AbstractHandler nextHandler){
        super.setNextHandler(nextHandler);
    }

    @Override
    public void doHandler(LogisticsData logisticsData) {
        this.logger.info("解析运单{}的接单结构",logisticsData.getWaybillNum());
        String orgcodeAccept = logisticsData.getOrgcodeAccept();
        Organization organizationAccept = this.organizationService.getByCode(orgcodeAccept);
         if(organizationAccept == null){
            this.logger.error("未知的运单配送站{}",orgcodeAccept);
            String errorReason = "未知的运单配送站{" + orgcodeAccept+ "}" + "该解析失败跳过";
            this.logisticsErrorLogService.addErrorLog(logisticsData,errorReason, BaseConstants.ANALYZE_ERROR_TYPE_ACCEPTORGANIZATION);
            this.getNextHandler().doHandler(logisticsData);
         }else{
             String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
             LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
             logisticsData2update.setOrgcodeAccept(organizationAccept.getOrgCode());
             try{
                 this.logisticsDataService.update(logisticsData2update);
                 this.updateDataVild(logisticsData2update, Privilege.B7);
                 this.getNextHandler().doHandler(logisticsData);
             } catch (Exception e) {
                 logger.error(e.getMessage(),e);
             }
         }
    }
}
